import datetime as dt
import pandas as pd
from collections import Counter
import matplotlib.pyplot as plt
import joblib
import numpy as np

# period
begin_date = dt.datetime(2022, 11, 1)
end_date = dt.datetime(2022, 11, 26)
period = "2022.11.01-2022.11.26"
pre_period = "2022.08.01-2022.10.31"
lang = "en"
processed_data_path = "data/%s_tokenized_%s.xlsx" % (period, lang)

# get_data
data = pd.read_excel(processed_data_path)
clf_path = "out/classifier_%s_%s.mdl" % (pre_period, lang)
lda_path = "out/lda_%s_%s.mdl" % (pre_period, lang)
feature_name_path = "out/feature_name_%s_%s.mdl" % (pre_period, lang)
feature_word_cnt = 500

# clustering
cluster_cnt = 8

# predict
predict_file_path = "out/predict_%s_%s.png" % (period, lang)

lda = joblib.load(lda_path)
feature_names = joblib.load(feature_name_path)
clf = joblib.load(clf_path)


def get_label(content):
    cnt_vec = [0] * feature_word_cnt
    for i in range(len(feature_names)):
        if feature_names[i] in content:
            cnt_vec[i] = 1
    lda_vec = lda.transform([cnt_vec])

    res = clf.decision_function(lda_vec)
    pre_labels = clf.predict(lda_vec)

    # print(res[0][pre_labels[0]])
    if abs(res[0][pre_labels[0]]) < 0.05:
        return cluster_cnt
    return pre_labels[0]


delta = dt.timedelta(days=1)
y_label = []
date_label = []
pre_sum = 0
accumulated_sum = []

while True:
    str_time = begin_date.strftime("%Y-%m-%d")
    date_label.append(str_time)
    day_labels = []
    for i in range(data.shape[0]):
        if data.loc[i]["time"].startswith(str_time):
            day_labels.append(get_label(data.loc[i]["tokenized"]))

    day_count = Counter(day_labels)
    day_cnt = []
    for j in range(cluster_cnt + 1):
        day_cnt.append(day_count[j])
    pre_sum += day_cnt[cluster_cnt]
    accumulated_sum.append(pre_sum)

    y_label.append(day_cnt)
    if begin_date == end_date:
        break
    begin_date += delta

xs = [dt.datetime.strptime(d, '%Y-%m-%d').date() for d in date_label]
plt.figure(figsize=(10, 6), dpi=100)
plt.tick_params(labelsize=8)
for i in range(cluster_cnt + 1):
    y = []
    for j in range(len(y_label)):
        y.append(y_label[j][i])

    if i == cluster_cnt:
        plt.plot(xs, y, label="new topic", linewidth=3)
    else:
        plt.plot(xs, y, label="topic %d" % (i + 1))

plt.plot(xs, accumulated_sum, label="number of new topics", linewidth=3)

plt.legend()
plt.ylabel("count")
plt.savefig(predict_file_path)
plt.show()
print(accumulated_sum)